#
# 1-1 只是加法
# # 嵌套
# # 引用
# # 返回
# def func_out(num1):
#     def fun_in(num2):
#         num = num1 + num2
#         print('num->', num)
#     return fun_in
#
# myf = func_out(10)
# myf(1)
# myf(1)
# myf(1)
# myf(1)
# pass

# 1-2 求和
#
# def func_out(num1):
#     num = num1
#     def fun_in(num2):
#         nonlocal num
#         num = num + num2
#         print('num->', num)
#     return fun_in
#
# myf = func_out(10)
# myf(1)
# myf(1)
# myf(1)
# myf(1)
# pass

# 1-3 闭包解方程
# y = ax + b
# def func_out(a, b):
#     def fun_in(x):
#         y = a*x + b
#         print('y->', y)
#     return fun_in
#
# myf = func_out(a=2, b=1)
# myf(1)
# myf(2)
# myf(3)



# 2-1: 手工装饰器
# def check(func):
#     def func_in():
#         print('请先登录...')
#         func()
#     return func_in
#
# def comment():
#     print('发表评论')
#
# if __name__ == '__main__':
#     comment = check(comment)
#     comment()



# 2-2 语法糖装饰器
# def check(func):
#     def func_in():
#         print('请先登录...')
#         func()
#     return func_in
#
#
# @check  # comment = check(comment)
# def comment():
#     print('发表评论')
#
# if __name__ == '__main__':
#     comment()


# 3-1 装饰无参无返回值
# def myprint_info(func):
#     def func_in():
#         print('正在努力计算...')
#         func()
#     return func_in
#
# @myprint_info  # 相当于  get_sum = print_info(get_sum)
# def get_sum():
#     a = 10
#     b = 10
#     c = a + b
#     print('两数之和:c', c)
#
# get_sum()


# # 3-2 装饰有参无返回值
# def myprint_info(func):
#     def func_in(a, b):
#         print('正在努力计算...')
#         func(a, b)
#     return func_in
#
# @myprint_info  # 相当于  get_sum = print_info(get_sum)
# def get_sum(a, b):
#     c = a + b
#     print('两数之和:c', c)
#
# get_sum(10, 20)


# # 3-2 装饰有参有返回值
# def myprint_info(func):
#     def func_in(a, b):
#         print('正在努力计算...')
#         mysum = func(a, b)
#         return mysum
#     return func_in
#
# @myprint_info  # 相当于  get_sum = print_info(get_sum)
# def get_sum(a, b):
#     c = a + b
#     print('两数之和:c', c)
#     return c
#
# mysum = get_sum(10, 20)
# print(mysum)


# # 3-2 装饰无参有返回值
# def myprint_info(func):
#     def func_in():
#         print('正在努力计算...')
#         mysum = func()
#         return mysum
#     return func_in
#
# @myprint_info  # 相当于  get_sum = print_info(get_sum)
# def get_sum():
#     a = 10
#     b = 20
#     c = a + b
#     print('两数之和:c', c)
#     return c
#
# mysum = get_sum()
# print(mysum)

# 3-3 不定长
def print_info(func):
    def inner(*args, **kwargs):
        sum = func(*args, **kwargs)
        return sum
    return inner


@print_info  #  get_sum = print_info(get_sum)
def get_sum(*args, **kwargs):
    sum = 0
    for arg in args:
        sum = sum + arg

    for value in kwargs.values():
        sum = sum + value
    return sum

if __name__ == '__main__':
    sum = get_sum(1, 2, 3, a=10, b=20, c=30)
    print(sum)
    pass



# 多个装饰
# 使用两个装饰器check_use  check_code 来装饰原函数

# def check_use(func):
#     def inner():
#         print('请用户登录...')
#         func()
#     return inner
#
# def check_code(func):
#     def inner():
#         print('检查验证码...')
#         func()
#     return inner
#
#
# # check_code check_use
# @check_use
# @check_code
# def check():  # check = check_code(check);   check = check_use(check)
#     print('发表评论...')
#
# check()

#
# # 4-1
# def decorator(myfun, flag):
#     def inner(a, b):
#         if flag == "+":
#             print('正在计算...+')
#         else:
#             print('正在计算...-')
#         ret = myfun(a, b)
#         return ret
#     return inner
#
#
# # 一个装饰器修饰过的函数
# @decorator('+')  # myadd = decorator(myadd)
# def myadd(a, b):
#     result = a + b
#     return result
#
# @decorator('-')  # mysub = decorator(mysub)
# def mysub(a, b):
#     result = a - b
#     return result



# 4-2 带参数的装饰器
def logging(flag):
    def decorator(myfun):
        def inner(a, b):
            if flag == "+":
                print('正在计算...+')
            else:
                print('正在计算...-')
            ret = myfun(a, b)
            return ret
        return inner
    return decorator


# 一个装饰器修饰过的函数
@logging('+')  # myadd = decorator(myadd)
def myadd(a, b):
    result = a + b
    return result

@logging('-')  # mysub = decorator(mysub)
def mysub(a, b):
    result = a - b
    return result





if __name__ == '__main__':
    res = myadd(10, 20)
    print(res)


